/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.commons.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.swing.JOptionPane;
import javax.swing.JProgressBar;

import jxl.Cell;
import jxl.Workbook;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TStock;

/**
 * 说明：
 * 商品导出功能使用类
 * @Author：李智深 
 * @Date：2012-04-16
 */
public class CommodityExportUtil{

	private static final long serialVersionUID = 1L;
	
	public static void writeExcel(String fileName,List<TCommodity> list,JProgressBar proBar){
		if (list !=null&&list.size()!=0) {
			WritableWorkbook wwb = null;
			try {
				// 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
				wwb = Workbook.createWorkbook(new File(fileName));
				if (wwb != null) {
					WritableSheet ws = wwb.createSheet("sheet1", 0);
					addRowTop(ws);//添加行头
					for (int i = 0; i < list.size(); i++) {
						TCommodity commodity=list.get(i);
						List<WritableCell> labelList=new ArrayList<WritableCell >();
						int n=0;
						labelList.add(new Label(n++, i+1,commodity.getCommodityID()));//商品编号
						labelList.add(new Label(n++, i+1,commodity.getBarcode()));//条形码
						labelList.add(new Label(n++, i+1,commodity.getName()));//商品名称
						labelList.add(new Label(n++, i+1,commodity.getPrice()+""));//价格
						labelList.add(new Number(n++, i+1,commodity.getDiscountPrice(),new jxl.write.WritableCellFormat(new jxl.write.NumberFormat("#.00" ))));//现价
						labelList.add(new Number(n++, i+1,commodity.getBatchPrice(),new jxl.write.WritableCellFormat(new jxl.write.NumberFormat("#.00" ))));//批发价
						labelList.add(new Label(n++, i+1,commodity.getDisCount()+""));//折扣率
						
						int number=0;
						Set<TStock> set=commodity.getTStocks();
						if(set!=null&set.size()!=0){
							Iterator<TStock> it=set.iterator();
							while(it.hasNext()){
								TStock stock=it.next();
								number=number+stock.getNum();
							}
						}
						labelList.add(new Number(n++, i+1,number));//库存量
						labelList.add(new Label(n++, i+1,commodity.getUnit()));//商品计量单位
						labelList.add(new Label(n++, i+1,commodity.getMadePlace()));//产地
						labelList.add(new Label(n++, i+1,commodity.getColor()));//颜色
						labelList.add(new Label(n++, i+1,commodity.getBatch()));//批次
						labelList.add(new Number(n++, i+1,commodity.getValidity()));//有效期
						labelList.add(new Label(n++, i+1,ApplicationContext.VALIDITY_MAP().get(commodity.getValidityUnit())));//有效期单位
						labelList.add(new Number(n++, i+1,commodity.getMemCode()));//助记码
						labelList.add(new Label(n++, i+1,commodity.getStd()));//产品规格
						labelList.add(new Label(n++, i+1,commodity.getSaleCount()+""));//销量
						labelList.add(new Label(n++, i+1,DateFormatUtil.formatF(commodity.getCreateTime())));//创建时间
						labelList.add(new Label(n++, i+1,DateFormatUtil.formatF(commodity.getUpdateTime())));//最后修改时间
						labelList.add(new Number(n++, i+1,commodity.getClickCount()));//点击率
						if(commodity.getStatus()==ApplicationContext.COMMODITY_STATUS_YES){
							labelList.add(new Label(n++, i+1,"已发布"));//1为已发布 2为未发布
						}else{
							labelList.add(new Label(n++, i+1,"未发布"));//1为已发布 2为未发布
						}
						if(commodity.getType()==ApplicationContext.COMMODITY_TYPE1){
							labelList.add(new Label(n++, i+1,"正常商品"));//1为正常商品 2为套餐
						}else{
							labelList.add(new Label(n++, i+1,"套餐"));//1为正常商品 2为套餐
						}
						labelList.add(new Number(n++, i+1,commodity.getPoint()));//附送积分
						labelList.add(new Label(n++, i+1,commodity.getDes()));//商品描述
						labelList.add(new Label(n++, i+1,commodity.getDetail()));//商品详细信息
						labelList.add(new Number(n++, i+1,commodity.getScore()));//好评度
						labelList.add(new Number(n++, i+1,commodity.getCommentCount()));//总评论数
						labelList.add(new Number(n++, i+1,commodity.getScoreCount()));//总评分次数
						for(int j=0;j<labelList.size();j++){
							ws.addCell(labelList.get(j));
						}
						ws.setRowView(i+1, 500);
						proBar.setValue(proBar.getValue()+1);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "另一个程序正在使用此文件，进程无法访问");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					// 从内存中写入文件中S
					wwb.write();
					// 关闭资源，释放内存
					wwb.close();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (WriteException e) {
					e.printStackTrace();
				}
			}
		}  
    }   
	
	public static void addRowTop(WritableSheet ws)throws Exception{
		
		WritableFont Bwf = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD, false); //创建大字体：Arial,大小为18号,粗体，非斜体 
		Bwf.setColour(jxl.format.Colour.BLACK); ////字体颜色为红色 
		//创建单元格格式：
		jxl.write.WritableCellFormat CwcfF = new jxl.write.WritableCellFormat(Bwf); 
		CwcfF.setAlignment(jxl.write.Alignment.CENTRE); //设置水平对齐为居中对齐 
		CwcfF.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直对齐为居中对齐 
		CwcfF.setBorder(jxl.format.Border.TOP, BorderLineStyle.MEDIUM); //设置顶部边框线为实线(默认是黑色－－也可以设置其他颜色) 
		CwcfF.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.MEDIUM);//设置右边框线为实线 
		CwcfF.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.MEDIUM); //设置顶部框线为实线 

		List<Label> labelList=new ArrayList<Label>();
		int n=0;
		labelList.add(new Label(n++, 0,"商品编号",CwcfF));//
		labelList.add(new Label(n++, 0,"条形码",CwcfF));//
		labelList.add(new Label(n++, 0,"商品名称",CwcfF));//
		labelList.add(new Label(n++, 0,"价格",CwcfF));//
		labelList.add(new Label(n++, 0,"现价",CwcfF));//
		labelList.add(new Label(n++, 0,"批发价",CwcfF));//
		labelList.add(new Label(n++, 0,"折扣率",CwcfF));//
		labelList.add(new Label(n++,0, "库存量",CwcfF));//
		labelList.add(new Label(n++, 0,"商品计量单位",CwcfF));//
		labelList.add(new Label(n++, 0,"产地",CwcfF));//
		labelList.add(new Label(n++, 0,"颜色",CwcfF));//
		labelList.add(new Label(n++, 0,"批次",CwcfF));
		labelList.add(new Label(n++, 0,"有效期",CwcfF));
		labelList.add(new Label(n++, 0,"有效期单位",CwcfF));
		labelList.add(new Label(n++, 0,"助记码",CwcfF));
		labelList.add(new Label(n++, 0,"产品规格",CwcfF));
		labelList.add(new Label(n++, 0,"销量",CwcfF));
		labelList.add(new Label(n++, 0,"创建时间",CwcfF));
		labelList.add(new Label(n++, 0,"最后修改时间",CwcfF));
		labelList.add(new Label(n++, 0,"点击率",CwcfF));
		labelList.add(new Label(n++, 0,"状态",CwcfF));//1为已发布 2为未发布
		labelList.add(new Label(n++, 0,"类型",CwcfF));//1为正常商品 2为套餐
		labelList.add(new Label(n++, 0,"附送积分",CwcfF));//
		labelList.add(new Label(n++, 0,"商品描述",CwcfF));//
		labelList.add(new Label(n++, 0,"商品详细信息",CwcfF));//
		labelList.add(new Label(n++, 0,"好评度",CwcfF));//
		labelList.add(new Label(n++, 0,"总评论数",CwcfF));//
		labelList.add(new Label(n++, 0,"总评分次数",CwcfF));//
		for(int j=0;j<labelList.size();j++){
			ws.addCell(labelList.get(j));
		}
		for(int i=0;i<ws.getColumns();i++){
			Cell cell=ws.getCell(i, 0);
			ws.setColumnView(i, cell.getContents().length()*4);
		}
//		ws.setRowView(0, 80*4);
	}
}
